package jp.wug.samples.export;

import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import com.google.inject.servlet.SessionScoped;

@RequestScoped
public class IndexPage {
    private final Tree tree;
    private final Status status;

    @Inject
    IndexPage(TreeRoot treeRoot, Status status) {
        tree = treeRoot.get();
        this.status = status;
    }

    public int getEdit() {
        return status.getEditingTreeId();
    }

    public void setEdit(int id) {
        status.setEditingTreeId(id);
    }

    public Tree getTree() {
        return tree;
    }

    @SessionScoped
    private static class TreeRoot {
        private final Tree tree;

        @Inject
        TreeRoot(TreeFactory treeFactory) {
            tree = treeFactory.create();
            tree.setName("Root");
            Tree childA = tree.addChild();
            childA.setName("ChildA");
            Tree leafA = childA.addChild();
            leafA.setName("LeafA");
            Tree childB = tree.addChild();
            childB.setName("ChildB");
        }

        Tree get() {
            return tree;
        }
    }
}
